package bandidong.dao.impl;

import bandidong.dao.CategoryDAO;
import bandidong.model.Category;
import bandidong.utils.Constant;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class CategoryDAOImpl implements CategoryDAO {
    
    private EntityManager em;
    
    public CategoryDAOImpl(EntityManager em) {
        this.em = em;
    }
    
    @Override
    public List<Category> getAllCategories() {
        try {
            String jpaql = "SELECT c FROM Category c";
            Query query = em.createQuery(jpaql);
            List<Category> categories = (List<Category>) query.getResultList();
            return categories;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public Category getCategoryById(int categoryId) {
        try {
            String jpaql = "SELECT c FROM Category c WHERE c.categoryId = :categoryId";
            Query query = em.createQuery(jpaql);
            query.setParameter("categoryId", categoryId);
            List<Category> categories = (List<Category>) query.getResultList();
            if (!categories.isEmpty()) {
                return categories.get(0);
            }
            return null;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public List<Category> getAllCategories(int page, int pageSize) {
        if (page <= 0 || pageSize <= 0) {
            return null;
        }
        try {
            String jpaql = "SELECT c FROM Category c";
            Query query = em.createQuery(jpaql);
            query.setFirstResult((page - 1) * pageSize);
            query.setMaxResults(pageSize);
            List<Category> categories = (List<Category>) query.getResultList();
            return categories;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public String addCategory(Category category) {
        try {
            em.persist(category);
            return Constant.SUCCESS;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public String updateCategory(Category category) {
        try {
            Category c = em.find(Category.class, category.getCategoryId());
            if (c != null) {
                c.setCategoryName(category.getCategoryName());
                c.setCategoryDescription(category.getCategoryDescription());
                em.merge(c);
                return Constant.SUCCESS;
            }
            return Constant.ERROR;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public String deleteCategory(Category category) {
        try {
            Category c = em.find(Category.class, category.getCategoryId());
            if (c != null) {
                em.remove(c);
                return Constant.SUCCESS;
            }
            return Constant.ERROR;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public Category getCategoryByName(String categoryName) {
        try {
            String jpaql = "SELECT c FROM Category c WHERE c.categoryName = :categoryName";
            Query query = em.createQuery(jpaql);
            query.setParameter("categoryName", categoryName);
            List<Category> categories = (List<Category>) query.getResultList();
            if (categories != null && !categories.isEmpty()) {
                return categories.get(0);
            }
            return null;
        }
        catch (Exception ex) {
            return null;
        }
    }
    
    @Override
    public boolean checkExists(String categoryName, int categoryId) {
        try {
            String jpaql = "SELECT c FROM Category c WHERE c.categoryName = :categoryName AND c.categoryId != :categoryId";
            Query query = em.createQuery(jpaql);
            query.setParameter("categoryName", categoryName);
            query.setParameter("categoryId", categoryId);
            List<Category> categories = (List<Category>) query.getResultList();
            if (!categories.isEmpty()) {
                return true;
            }
            return false;
        }
        catch (Exception ex) {
            return true;
        }
    }
}
